<?php

class BDDManager {

    private $host    = "localhost";
    private $user    = "root";
    private $passwd  = "j8mt5bg3";
    //private $passwd  = "";
    private $bddName = "warmaclock";
    private $dsn; // host + database
    private $con; //type PDO

    /* var $host="mysql51-81.perso";
      var $user="ericgruxdrupal";
      var $passwd="coucou1664";
      var $bddName="ericgruxdrupal"; */
    var $isConnected = false;

    public function __construct() {
        $num_args = func_num_args();
        switch ($num_args) {
            case 3: {
                    $this->host   = func_get_arg(0);
                    $this->user   = func_get_arg(1);
                    $this->passwd = func_get_arg(2);
                };
                break;
        }
        $this->dsn    = "mysql:host=" . $this->host . ";dbname=" . $this->bddName;
    }

    // SELECT query
    public function query($aQuery) {
        //$result = mysql_query($aQuery);
        $result = $this->con->query($aQuery);
        return $result;
    }

    // UPDATE | DELETE query
    public function exec($aReq) {
        $numRow = $this->con->exec($aReq);
        return $numRow;
    }

    // prepare statement
    public function prepare($aStr) {
        $statement = $this->con->prepare($aStr);
        return $statement;
    }

    //begin Transaction
    public function beginTransaction() {
        $this->con->beginTransaction();
    }

    // commit
    public function commit() {
        return $this->con->commit();
    }

    //roll back
    public function rollBack() {
        return $this->con->rollBack();
    }

    // Error Message
    function errorMsg() {
        $error = $this->con->erroInfo();
        echo 'Error: ';
        foreach ($error as $value) {
            echo $value . ' ';
        }
    }

    // Connection BDD
    function connectBDD() {
        try {
            $this->con         = new PDO($this->dsn, $this->user, $this->passwd);
            $this->isConnected = true;
        } catch (Exception $e) {
            die('Erreur : ' . $e->getMessage());
        }
    }

    function isConnected() {
        return $this->isConnected;
    }

}

GLOBAL $bdd;
$bdd = new BDDManager();
?>